package com.houtai.controller.index;


import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.bean.TsJzAdmin;
import com.houtai.common.util.Ip;
import com.houtai.service.funcion.FuncionService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = {"v5"})
public class IndexController {
    private static Logger logger = Logger.getLogger(IndexController.class);
    @Autowired
    private FuncionService funcionService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * @Purpose   跳转到登录页面
     * @param
     * @version   1.0
     * @author    李寻欢
     * @time      2018-07-09
     * @return    ModelAndView
     */
    @RequestMapping(value = "/back/manager/login")
    @Authority(AuthorityType.NoValidate)
    public ModelAndView login(HttpServletRequest request) {
        String ip = Ip.getIpAddr(request);
//        if ("183.130.234.84, 115.205.217.246".contains(ip)) {
            ModelAndView mv = new ModelAndView("login");
            String token = request.getParameter("token");
            if (token != null) {
                redisTemplate.delete(token);
            }
            return mv;
//        } else {
//            logger.info("ip不在白名单内*" + ip);
//            return null;
//        }
    }
    /**
     * @Purpose   跳转到首页
     * @param
     * @version   1.0
     * @author    李寻欢
     * @time      2018-07-09
     * @return    ModelAndView
     */
    @RequestMapping(value = "/back/manager/index")
    @Authority(AuthorityType.Login)
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("index");
        TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
        mv.addObject("token", request.getParameter("token"));
        mv.addObject("user_name", tsJzAdmin.getUser_name());
        mv.addObject("retInfo", funcionService.findFatherFuncion(tsJzAdmin));
        return mv;
    }
    /**
     * @Purpose   跳转到欢迎页
     * @param
     * @version   1.0
     * @author    李寻欢
     * @time      2018-07-09
     * @return    ModelAndView
     */
    @RequestMapping(value = "/back/manager/welcome")
    @Authority(AuthorityType.Login)
    public ModelAndView welcome(HttpServletRequest request) {

        ModelAndView mv = new ModelAndView("welcome");
        mv.addObject("token", request.getParameter("token"));
        return mv;
    }

}